// 导入
const fs = require('fs')
const path = require('path')
const XLSX = require('xlsx');
//1.读取data.json文件
fs.readFile(path.join(__dirname, 'test.json'), 'utf8', (err, data) => {
    if (err) {
        console.log(err)
        return
    }
    // console.log(data);
    //2.将字符串数据转变成数组格式
    let arr = JSON.parse(data);
    let raw = [['店铺名','类目','标题','链接','价格','发货地','付款人数','旺旺id']];
    //存放新的数据
    //测试更改
    for (let i = 0; i < arr.length; i++) {

        let detail_url = arr[i].detail_url.replace('//', 'http://');//链接不全我替换一下。
        raw[i+1] = [arr[i].shopName, arr[i].category, arr[i].raw_title,detail_url, arr[i].view_price, arr[i].item_loc, arr[i].view_sales, arr[i].nick];
    }

    const workbook = XLSX.utils.book_new();
    const worksheet = XLSX.utils.aoa_to_sheet(raw);//需要的就是一个数组，我们已经拿到了数组，直接丢给他就可以了
    XLSX.utils.book_append_sheet(workbook,worksheet,'sheet1');
    const filename = 'taobao_rank.xlsx'
    XLSX.writeFile(workbook,filename);
    console.log(`File $ffilenamer is written successfully `);
})